package com.kitty.game.task.service.taskHandler;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.config.TaskSet;
import com.kitty.game.pk.service.PkService;
import com.kitty.game.role.model.Role;
import com.kitty.game.task.TaskDataPool;
import com.kitty.game.task.message.vo.TaskInfo;
import com.kitty.game.task.model.product.TaskType;
import com.kitty.game.utils.Const;
import com.kitty.game.utils.TimeUtil;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;

/**
 * 强制PK任务
 */
@Component
public class ForcePkTaskHandler extends TimeTaskHandler {
    private static final String TASK_JIESHAO_TIME = "你当前处于红名状态且拥有#R{0}#n点PK值，受到大家排斥，在各药店、杂货店以及便捷购买入口购买物品时，价格会变高。\r\n参与有死亡惩罚的战斗死亡一次可以减少1点的PK值，红名状态下无法清除PK值。红名剩余时间:#R{1}#n分";
    private static final String TASK_JIESHAO_PKVALUE = "你当前拥有{0}点PK值，受到大家排斥，在各药店、杂货店以及便捷购买入口购买物品时，价格会更高。\r\n参与有死亡惩罚的战斗死亡一次可以减少1点的PK值，还可以去#Y无念僧#n处使用好心值与金钱减少身上的PK值。";

    private static final String TASK_ZHIYIN_TIME = "红名剩余时间:#R{0}#n分\r\n#R前往安全区会被关入监狱#n";
    private static final String TASK_ZHIYIN_PKVALUE = "你当前身上的PK值为:#Y{0}#n";

    @Override
    public TaskType getTaskType() {
        return TaskType.FORCE_PK;
    }

    @Override
    public boolean canResetTask(Role role, TaskSet taskSet) {
        return super.canResetTask(role, taskSet);
    }

    @Override
    public TaskInfo getTaskInfo(Role role, TaskSet taskSet) {
        switch (taskSet.getId()) {
            case TaskDataPool.KILL_TASKID:
                return getKillTaskInfo(role, taskSet);
            case TaskDataPool.RED_TASKID:
                return getRedTaskInfo(role, taskSet);
            case TaskDataPool.PRISON_TASKID:
                return getPrisonTaskInfo(role, taskSet);
        }
        return null;
    }

    private TaskInfo getKillTaskInfo(Role role, TaskSet taskSet) {
        TaskInfo taskInfo = taskService.getTaskInfo(role, taskSet);
        long acceptTime = role.getTask().getAcceptTime(taskSet.getId());
        taskInfo.setTask_end_time((int) ((acceptTime + Const.PRISON_MINUTE * TimeUtil.ONE_MINUTE) / TimeUtil.ONE_SECOND));
        taskInfo.setRefresh((short) 1);
        int remainMinute = (int) (Const.PRISON_MINUTE - ((System.currentTimeMillis() - acceptTime) / TimeUtil.ONE_MINUTE));
        taskInfo.setTaskZhiyin(MessageFormat.format(taskInfo.getTaskZhiyin(), remainMinute));
        taskInfo.setTaskInfo(MessageFormat.format(taskInfo.getTaskInfo(), remainMinute));

        return taskInfo;
    }

    private TaskInfo getRedTaskInfo(Role role, TaskSet taskSet) {
        TaskInfo taskInfo = taskService.getTaskInfo(role, taskSet);

        String taskJieshao = null;
        String taskZhiyin = null;
        if (role.getPkInfoBox().getRedTime() > 0) {
            int remainTime = role.getPkInfoBox().getRedTime() / 60;
            taskJieshao = MessageFormat.format(TASK_JIESHAO_TIME,role.getPkInfoBox().getPkValue(), remainTime);
            taskZhiyin = MessageFormat.format(TASK_ZHIYIN_TIME, remainTime);
        } else {
            taskJieshao = MessageFormat.format(TASK_JIESHAO_PKVALUE, role.getPkInfoBox().getPkValue());
            taskZhiyin = MessageFormat.format(TASK_ZHIYIN_PKVALUE, role.getPkInfoBox().getPkValue());
        }

        taskInfo.setTaskInfo(taskJieshao);
        taskInfo.setTaskZhiyin(taskZhiyin);

        return taskInfo;
    }

    private TaskInfo getPrisonTaskInfo(Role role, TaskSet taskSet) {
        if (role.getPkInfoBox().getPrisonTime() > 0) {
            TaskInfo taskInfo = taskService.getTaskInfo(role, taskSet);
            taskInfo.setShow_name("坐牢");
            int remainTime = (int)((role.getPkInfoBox().getPrisonTime()-(System.currentTimeMillis()/1000))/60);
            taskInfo.setTaskInfo(MessageFormat.format(taskInfo.getTaskInfo(), remainTime));
            taskInfo.setTaskZhiyin(MessageFormat.format(taskInfo.getTaskZhiyin(), remainTime));

            return taskInfo;
        }

        return null;
    }

    @Override
    public void handleHeartBeat(Role role, long prevHeartTime) {
        long now = System.currentTimeMillis();
        PkService pkService = SpringUtils.getBean(PkService.class);
        if (prevHeartTime > 0) {
            if (role.getPkInfoBox().getRedTime() > 0 && !pkService.isInPrisonTime(role)) {
                int sec = (int) ((now - prevHeartTime) / TimeUtil.ONE_SECOND);
                int newRedTime = role.getPkInfoBox().getRedTime() - sec;
                role.getPkInfoBox().setRedTime(Math.max(newRedTime, 0));
                if (role.getPkInfoBox().getRedTime() <= 0) {
                    role.getPkInfoBox().setRedName(false);
                    SpringUtils.getBroadcastService().sendUpdateAppear(role);
                }
                role.save();
            }

//            if (role.getPkInfoBox().getPrisonTime() > 0) {
//                int sec = (int) ((now - prevHeartTime) / TimeUtil.ONE_SECOND);
//                int newPrisonTime = role.getPkInfoBox().getPrisonTime() - sec;
//                role.getPkInfoBox().setPrisonTime(Math.max(newPrisonTime, 0));
//                if (role.getPkInfoBox().getPrisonTime() <= 0) {
//                    /**放出监狱*/
//                    pkService.removeFromPrison(role);
//                }
//                role.save();
//            }
        }

        TaskSet taskSet = taskService.getTaskSet(TaskDataPool.KILL_TASKID, role);
        if (taskService.isTaskAccepted(role, taskSet)) {
            long acceptTime = role.getTask().getAcceptTime(taskSet.getId());
            long expireTime = acceptTime + (Const.PRISON_MINUTE * TimeUtil.ONE_MINUTE);
            if (System.currentTimeMillis() >= expireTime) {
                forceResetTask(role, taskSet);
            } else {
                taskService.pushTaskInfo(role, getTaskInfo(role, taskSet));
            }
        }

        taskSet = taskService.getTaskSet(TaskDataPool.RED_TASKID, role);
        if (taskService.isTaskAccepted(role, taskSet)) {
            if (role.getPkInfoBox().getRedTime() <= 0 && role.getPkInfoBox().getPkValue() <= 0) {
                forceResetTask(role, taskSet);
            } else {
                taskService.pushTaskInfo(role, getTaskInfo(role, taskSet));
            }
        }

        taskSet = taskService.getTaskSet(TaskDataPool.PRISON_TASKID, role);
        if (taskService.isTaskAccepted(role, taskSet)) {
            if (role.getPkInfoBox().getPrisonTime() <= 0) {
                forceResetTask(role, taskSet);
            } else {
                taskService.pushTaskInfo(role, getTaskInfo(role, taskSet));
            }
        }
    }
}
